//
//  HJRepairGroupSectionView.m
//  GAMCAfterSales
//
//  Created by 贺佳 on 2018/7/30.
//  Copyright © 2018年 glendale. All rights reserved.
//

#import "HJRepairGroupSectionView.h"

@implementation HJRepairGroupSectionView
-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithReuseIdentifier:reuseIdentifier]) {
        
    }
    return self;
}
-(void)setTitleAry:(NSArray *)titleAry
{
    _titleAry=titleAry;
    
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
   
    for (NSString*str in titleAry) {
        UIButton*titleLabel=[[UIButton alloc]init];
        titleLabel.titleLabel.font=HJFont(13);
        [titleLabel setTitle:str forState:UIControlStateNormal];
        [titleLabel setTitleColor:COLOR_(@"#333333") forState:UIControlStateNormal];
        titleLabel.backgroundColor=COLOR_(@"#eeeeee");
        if ([str isEqualToString:@"工时单价"]) {
           [titleLabel setBackgroundImage:[UIImage imageNamed:@"10.png"] forState:UIControlStateNormal];
            [titleLabel addTarget:self action:@selector(clickSelect:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self.contentView addSubview:titleLabel];
        
        
    }
    
    WEAKSELF;
    [self.contentView.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
    [self.contentView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(weakSelf.contentView);
    }];
}
-(void)clickSelect:(UIButton*)btn{
    if ([_delegate respondsToSelector:@selector(repairGroupSectionView:clickBtn:)]) {
        [_delegate repairGroupSectionView:self clickBtn:btn];
    }
}
-(void)setFlexAry:(NSArray *)flexAry
{
    _flexAry=flexAry;
    UIButton*lastLabel;//记录上一个label
    NSInteger totalFlex = 0;
    for (NSString*flex in flexAry) {
        totalFlex+=[flex integerValue];
    }
    WEAKSELF;
    for (int i =0; i<self.contentView.subviews.count; i++) {
        UIButton*label=self.contentView.subviews[i];
        if (i<flexAry.count) {
            NSInteger currentFlex=[flexAry[i] integerValue];
            [label mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(lastLabel?lastLabel.mas_right:weakSelf.contentView).offset(lastLabel?1:0);
                make.top.bottom.mas_equalTo(weakSelf.contentView);
                make.width.mas_equalTo(weakSelf.contentView).multipliedBy(currentFlex*1.0/totalFlex);
            }];
        }else{
            [label mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(lastLabel?lastLabel.mas_right:weakSelf.contentView).offset(lastLabel?1:0);
                make.top.bottom.mas_equalTo(weakSelf.contentView);
                make.width.mas_equalTo(weakSelf.contentView).multipliedBy(totalFlex*1.0/flexAry.count);
            }];
        }
        lastLabel=label;
    }
}

@end
